<template>
  <div>
    <div style="margin-bottom: 10px;">
      <el-button type="primary" icon="el-icon-edit" @click="choose">选择仓库</el-button>
      <el-button @click="add" v-if="stockId" icon="el-icon-plus">新增入库</el-button>
    </div>
    <el-table :data="list">
      <el-table-column type="index" label="序号"/>
      <el-table-column label="商品ID" prop="goods_id"/>
      <el-table-column label="商品名" prop="goods_name"/>
      <el-table-column label="商品库存" prop="num"/>
      <el-table-column label="采购价" prop="price"/>
      <el-table-column label="供应商" prop="provider"/>
      <el-table-column label="入库时间" prop="created"/>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="moveChoose(scope.row)">调拨</el-button>
          <el-button type="text" @click="outStock(scope.row)">出库</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align: right; margin-top: 10px;">
      <el-pagination
        small
        class="page"
        @current-change="load"
        :current-page.sync="pageNumber"
        :page-size="20"
        layout="prev, pager, next, jumper"
        :total="total || 0"
      ></el-pagination>
    </div>
    <stock-choose ref="stockChoose" @ok="onChoose"/>
    <stock-choose ref="moveStockChoose" @ok="onMoveChoose"/>
    <stock-in-dialog ref="stockInDialog" @ok="load"/>
  </div>
</template>

<script>
    import StockChoose from "./stockChoose";
    import StockInDialog from "./stockInDialog";
    export default {
        name: "stockIn",
      components: {StockInDialog, StockChoose},
      data(){
          return {
            list: [],
            total: 0,
            stockId: "",
            pageNumber: 1,
          }
      },

      methods:{
          add(){
            this.$refs.stockInDialog.show(this.stockId)
          },
          load(){
            // stockDetailPage
            this.$http({
              url: this.$http.adornUrl('/sys/storage/stockDetailPage'),
              method: 'get',
              params: this.$http.adornParams(
                {
                  page: this.pageNumber,
                  stockId: this.stockId
                }
              )
            }).then(({data}) => {
              this.list = data.list
              this.total = Number(data.total)
            })

          },
        choose(){
          this.$refs.stockChoose.show()
        },
        onChoose(v){
          this.stockId = v
          this.load()
        },
        moveChoose(v){
            this.itemId = v.id
            this.$refs.moveStockChoose.show()
        },
        onMoveChoose(v){
            if(v == this.stockId){
              this.$message.info("不能选择相同的仓库")
              return
            }
          this.$http({
            url: this.$http.adornUrl('/sys/storage/moveStockDetail'),
            method: 'get',
            params: this.$http.adornParams(
              {
                id: this.itemId,
                newStockId: v
              }
            )
          }).then(({data}) => {
            this.$message.success("调拨成功")
            this.load()
          })

        },


        outStock(row){
          this.$prompt('请输入出库数量', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            inputPattern: /^\d+$/,
            inputErrorMessage: '出库数量不正确'
          }).then(({ value }) => {
            var v = Number(value)
            if(v > Number(row.num)){
              this.$message.error("出库数量不能大于库存")
            }
            this.$http({
              url: this.$http.adornUrl('/sys/storage/outStockDetail'),
              method: 'get',
              params: this.$http.adornParams(
                {
                  id: row.id,
                  num: v
                }
              )
            }).then(({data}) => {
              this.$message.success("出库成功")
              this.load()
            })
          }).catch(() => {
          });
        }
      },
      mounted() {
          this.choose()
      }
    }
</script>

<style scoped>

</style>
